package com.bl.andr.utillib.time;

import com.bl.andr.utillib.ValueOf;

import java.util.Date;

/**
 * DESC: 其他的一些逻辑
 * TIME:2019-06-24
 */
public class TimeUtil {

    /**
     * 获得天数差
     *
     * @param begin 开始日期
     * @param end   结束日期
     */
    public int computeDateDiff(Date begin, Date end) {
        int day = 0;
        if (end.getTime() < begin.getTime()) {
            day = -1;
        } else {
            day += (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000);
        }
        return day;
    }

    /**
     * 转化时间差为提示性的字符串
     *
     * @param timestamp 给定的时间戳（秒）
     * @return 返回与现在时间相差的时间
     */
    public static String convertTimeGap(long timestamp) {
        long currentSeconds = DateUtil.nowSec();
        long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
        if (timeGap < 60 && timeGap >= 0) {
            return "刚刚";
        } else if (timeGap >= 60 && timeGap < 3600) {
            return timeGap / 60 + "分钟前";
        } else if (timeGap >= 3600 && timeGap < 3600 * 24) {
            return timeGap / 3600 + "小时前";
        } /*else if (timeGap >= 3600 * 24 && timeGap < 3600 * 24 * 30) {
            return timeGap / 3600 / 24 + "天前";
        } else if (timeGap >= 3600 * 24 * 30 && timeGap < 3600 * 24 * 30 * 12) {
            return timeGap / 3600 / 24 / 30 + "个月前";
        } else if (timeGap >= 3600 * 24 * 30 * 12) {
            return timeGap / 3600 / 24 / 30 / 12 + "年前";
        }*/
        return "";
    }


    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     */
    public static String convertTimeToFormat(long time) {
        long m = 0;
        long s = 0;
        if (time < 60) {
            m = 0;
            s = time;
        } else if (time < 3600) {
            m = time / 60;
            s = time % 60;
        }
        StringBuilder builder = new StringBuilder();
        if (m < 10) builder.append("0").append(m);
        else builder.append(m);
        builder.append(":");
        if (s < 10) builder.append("0").append(s);
        else builder.append(s);

        return builder.toString();
    }


    /* ****
     * 毫秒转化时分秒
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
//        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day).append("天");
        }
        if (hour > 0) {
            sb.append(hour).append("时");
        }
        if (minute > 0) {
            sb.append(minute).append("分");
        }
        if (second > 0) {
            sb.append(second).append("秒");
        }
       /* if(milliSecond > 0) {
            sb.append(milliSecond+"毫秒");
        }*/
        return sb.toString();
    }


    /**
     * 计算两个时间间隔
     *
     * @param sTime
     * @param eTime
     * @return
     */
    public static String cdTime(long sTime, long eTime) {
        long diff = eTime - sTime;
        return diff > 1000 ? diff / 1000 + "秒" : diff + "毫秒";
    }

    /**
     * 判断两个时间戳相差多少秒
     *
     * @param d
     * @return
     */
    public static int dateDiffer(long d) {
        try {
            long l1 = ValueOf.toLong(
                    String.valueOf(System.currentTimeMillis()).substring(0, 10)
            );
            long interval = l1 - d;
            return (int) Math.abs(interval);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

}
